<?php
class TransferAction extends MemberAction{
    //转账模块
	public function index(){
		//转账的类型定为9999
		//显示列表
		$pagesize = 30;
		//如果用户指定分页大小，则使用用户指定的数量进行分页
        if ($this->_get('pagesize')) {
            $pagesize = intval($this->_get('pagesize'));
        }
		
        $listmap = 's.site_id = '.__SITEID__." and pdstate = '9999' and member_id = ".$this->memberInfo['id'];
		
		
		
		//实例化供货商表模型，以便进行数据读取
        $BranddB = D('Member_account_log');
		//是否有搜索关键词提交上来
		if(isset($_GET['keywords'])){
			//用户可以通过店名搜索到记录
			$listmap .= " and remark like '%".($_GET['keywords'])."%'";
		}
		//计算全部数据个数，以便进行分页
		$count = $BranddB->alias('s')->where($listmap)->count();
		//引入分类类
        import('ORG.Util.Page');
		//实例化分页类
        $p = new Page($count, $pagesize);
		//开始分页查询
        $list = $BranddB->alias('s') -> Field('s.*') ->where($listmap)->limit($p->firstRow . ',' . $p->listRows) -> order($order) -> select();
		//生成分页字符串
		$pages = $p->show();
		//echo $BranddB -> getLastSql();
		//dump($list);
		//填充数据列表
		$this->assign('list', $list);
		//填充分页字符
		$this->assign('pages', $pages);
		//显示页面
		$this->display();
	}
	
	//显示会员列表，查询转账目标
	
	public function memberlist(){
		//转账的类型定为9999
		//显示列表
		$pagesize = 10;
		//如果用户指定分页大小，则使用用户指定的数量进行分页
        if ($this->_get('pagesize')) {
            $pagesize = intval($this->_get('pagesize'));
        }
		
        $listmap = 's.site_id = '.__SITEID__." and id <> '".$this->memberInfo['id']."'";
		
		if(isset($_GET['keywords'])){
			
			if($_GET['keywords'] == ''){
				$this->error('请输入会员ID或者昵称搜索！');
			}
			$listmap .= " and (nickname like '%".$_GET['keywords']."%' or id = '".intval($_GET['keywords'])."')";
		}
		
		//实例化供货商表模型，以便进行数据读取
        $BranddB = D('Member');
		//是否有搜索关键词提交上来
		
		//计算全部数据个数，以便进行分页
		$count = $BranddB->alias('s')->where($listmap)->count();
		//引入分类类
        import('ORG.Util.Page');
		//实例化分页类
        $p = new Page($count, $pagesize);
		//开始分页查询
        $list = $BranddB->alias('s') -> Field('s.*') ->where($listmap)->limit($p->firstRow . ',' . $p->listRows) -> order($order) -> select();
		//生成分页字符串
		$pages = $p->show(); 
		//echo $BranddB -> getLastSql();
		//dump($list);
		//填充数据列表
		//dump($list);
		$this->assign('list', $list);
		//填充分页字符
		$this->assign('pages', $pages);
		//显示页面
		$this->display();
	}
	//转账
	public function form(){
		$affobj = $payment = M('Affiliatesetting') -> where(array('site_id' => __SITEID__, 'status' => 1)) -> find();
		if($affobj){
			import("@.ORG.Affiliate.".$affobj['classname']);
			$class = $affobj['classname'];
			$Threelevel = new $class(0,__SITEID__,$this->memberInfo['id']);
			$affdata = $Threelevel -> getAffData();
		}
		$functionex = method_exists($Threelevel,'transfer');
		if(!$functionex){
			$this->error('系统不支持转账！');
			exit;
		}
		if(IS_POST){
			$tomember_id =  $this->_post('tomember_id');
			$money = $this->_post('money');
			$ret = $Threelevel -> transfer($tomember_id, $money);
			if(!$ret){
				$this->error($Threelevel -> getError());
				exit;
			}
			$this->success('操作成功！');
			exit;
		}else{
			
			$tomember_id = intval($this -> _get('member_id'));
			if($tomember_id == 0){
				$this->error('参数错误！');
			}
			if($tomember_id == $this->memberInfo['id']){
				$this->error('不能给自己转账！');
			}
			$tomember = M('Member') -> where(array('site_id' => __SITEID__, 'id' => $tomember_id)) -> find();
			if(!$tomember ){
				$this->error('找不到转账目标！');
			}
			
			$this->tomember = $tomember;
			$this->money = M('Member')->where(array('id' => $this->memberInfo['id'])) -> getField('money');
			$this->display();
		}
	}
}